﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RaiderManagerBackend;
using Telerik.Windows.Controls;
using RaiderManager.EditWindows;
using System.Collections.ObjectModel;

namespace RaiderManager
{
    /// <summary>
    /// Interaction logic for MainRadWindow.xaml
    /// </summary>
    public partial class MainRadWindow : Window
    {
        public static ObservableCollection<Player> players = BusinessLogic.GetPlayers();
        public static ObservableCollection<Raid> raids = BusinessLogic.GetRaids();
        public MainRadWindow()
        {
            InitializeComponent();
            this.PlayerList.DataContext = players;
            this.RaidList.DataContext = raids;
        }

        private void PlayerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems[0] is Player)
            {
                Player p = (Player)e.AddedItems[0];
                p.BuildAttendance();
                p.BuildPerformance();
                this.AttendanceHeader.Text = string.Format("{0} Attendance. Total Points: {1} -- Grade: {2}", p.Name, p.RecentAttendancePoints, p.AttendanceGrade);
                this.PerformanceHeader.Text = string.Format("{0} Performance. Average Variance: {1} -- Grade: {2}", p.Name, p.AverageTotalPerformance, p.PerformanceGrade);
                this.ParticipatedCount.Text = p.NumberOfParticipated.ToString();
                this.ParticipatedPoints.Text = (p.NumberOfParticipated * Enumerations.AttendancePoints.Participated).ToString();
                this.BenchedCount.Text = p.NumberOfBenched.ToString();
                this.BenchedPoints.Text = (p.NumberOfBenched * Enumerations.AttendancePoints.Benched).ToString();
                this.LateCount.Text = p.NumberOfLates.ToString();
                this.LatePoints.Text = (p.NumberOfLates * Enumerations.AttendancePoints.Late).ToString();
                this.NoShowCount.Text = p.NumberOfNoShows.ToString();
                this.NoShowPoints.Text = (p.NumberOfNoShows * Enumerations.AttendancePoints.NoShow).ToString();
                this.AvailableCount.Text = p.NumberOfSignUpAvailable.ToString();
                this.AvailablePoints.Text = (p.NumberOfSignUpAvailable * Enumerations.AttendancePoints.SignUpAvailable).ToString();
                this.UnavailableCount.Text = p.NumberOfSignupUnavailable.ToString();
                this.UnavailablePoints.Text = (p.NumberOfSignupUnavailable * Enumerations.AttendancePoints.SignUpUnavailable).ToString();
                this.PlayerAttendanceEntries.DataContext = p.AttendanceEntries;
                this.PlayerPerformanceEntries.DataContext = p.PerformanceEntries;

            }
        }

        private void EditAttendance_Click(object sender, RoutedEventArgs e)
        {
            AttendanceEntry ae = null;
            RadTabItem selectedTab = (RadTabItem)this.MainWindowTab.SelectedItem;
            if (selectedTab.Header.Equals("Players"))
            {
                ae = (AttendanceEntry)this.PlayerAttendanceEntries.SelectedItem;
            }
            else
            {
                ae = (AttendanceEntry)this.RaidAttendanceEntries.SelectedItem;
            }
            MessageBox.Show(string.Format("{0} attendance. {1} | {2}", ae.Player.Name, ae.AttendanceCode, ae.SignUpCode));
        }

        private void PlayerEditPerformance_Click(object sender, RoutedEventArgs e)
        {
            PlayerPerformanceEntry pe = null;
            RadTabItem selectedTab = (RadTabItem)this.MainWindowTab.SelectedItem;
            if (selectedTab.Header.Equals("Players"))
            {
                pe = (PlayerPerformanceEntry)this.PlayerPerformanceEntries.SelectedItem;
            }
            else
            {
                pe = (PlayerPerformanceEntry)(e.Source as Button).DataContext;
            }

            EditPerformanceEntryWindow performanceWindow = new EditPerformanceEntryWindow(pe);
            performanceWindow.ShowDialog();
        }

        private void RaidList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (e.AddedItems[0] is Raid)
            {
                Raid r = (Raid)e.AddedItems[0];
                r.BuildAttendance();
                r.BuildEncounters();

                this.RaidAttendanceEntries.DataContext = r.AttendanceEntries;
                this.RaidEncountersPanelBar.ItemsSource = r.Bosses;
            }
            else
            {
                MessageBox.Show("Break ALL the things?");
            }
            
        }

        private void editSelectedPlayerButton_Click(object sender, RoutedEventArgs e)
        {
            Player p = (Player)this.PlayerList.SelectedItem;
            if (p != null)
            {
                AddEditPlayerWindow editPlayerWindow = new AddEditPlayerWindow((Player)this.PlayerList.SelectedItem);
                editPlayerWindow.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please select a player before clicking the edit button");
            }
        }

        private void addNewPlayerButton_Click(object sender, RoutedEventArgs e)
        {
            AddEditPlayerWindow newPlayerWindow = new AddEditPlayerWindow();
            Player p = newPlayerWindow.ShowDialog();
            if (p != null)
            {
                players.Add(p);
            }
        }

        private void editRaidButton_Click(object sender, RoutedEventArgs e)
        {
            Raid r = (Raid)this.RaidList.SelectedItem;
            if (r != null)
            {
                AddEditRaidWindow editRaidWindow = new AddEditRaidWindow(r);
                editRaidWindow.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please select a raid to edit before clicking the edit button");
            }
        }

        private void addRaidButton_Click(object sender, RoutedEventArgs e)
        {
            AddEditRaidWindow addRaidWindow = new AddEditRaidWindow();
            Raid r = addRaidWindow.ShowDialog();
            if (r != null)
            {
                raids.Add(r);
            }
        }


    }
}
